package com.yootk.rabbitmq.producer.direct;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;

import java.io.IOException;

public class MessageProducerDirect {
    private static final String ROUTING = "yootk.routingkey.direct" ;
    private static final String HOST = "rabbitmq-server" ;
    private static final int PORT = 5672 ;
    private static final String USERNAME = "yootk" ;
    private static final String PASSWORD = "hello" ;
    private static final String VHOST = "MuyanVHost" ;
    private static final String EXCHANGE_NAME ="yootk.exchange.direct" ;

    public static void main(String[] args) throws Exception{
        ConnectionFactory factory = new ConnectionFactory() ;
        factory.setHost(HOST);
        factory.setPort(PORT);
        factory.setUsername(USERNAME);
        factory.setPassword(PASSWORD);
        factory.setVirtualHost(VHOST);
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();
        Long start = System.currentTimeMillis() ;
        channel.exchangeDeclare(EXCHANGE_NAME,"direct") ;
        for (int x = 0 ; x < 5 ; x ++){
            int temp = x ;
            new Thread(() ->{
                String msg = "【科技-" + temp + "】张三" ;
                try {
                    channel.basicPublish(EXCHANGE_NAME,ROUTING, MessageProperties.PERSISTENT_TEXT_PLAIN,msg.getBytes());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            },"消息生产者 - " + x).start();
        }
        Long end = System.currentTimeMillis() ;
        System.out.println("消费的时间是：" + (end - start));
        channel.close();
        connection.close();
    }
}
